import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/entrys/rp/return_booking_entity.dart';
import 'package:xiongan_app_yy/provider/erp_info.dart';

class CountButtonReturn extends StatefulWidget {
  ReturnBookingOutorderdetaillist item;
  int index;
  ReturnBookingOutorderdetaillist itemNew;

  CountButtonReturn(this.item, this.index, this.itemNew);

  @override
  State<StatefulWidget> createState() {
    return CountButtonReturnState();
  }
}

class CountButtonReturnState extends State<CountButtonReturn> {
  TextEditingController controller = TextEditingController();

  @override
  void initState() {
    widget.item.realquantity = widget.itemNew.realquantity;
    controller.text = widget.item.realquantity.toString();
  }

  @override
  Widget build(BuildContext context) {
    controller.selection = TextSelection.fromPosition(TextPosition(
        affinity: TextAffinity.downstream, offset: controller.text.length));
    return Container(
      margin: EdgeInsets.only(top: 5.0),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
          border:
              Border.all(width: 1, color: Colors.black12) //设置所有的边框宽度为1 颜色为浅灰
          ),
      child: Row(
        children: <Widget>[
          _reduceBtn(context),
          _countArea(context),
          _addBtn(context)
        ],
      ),
    );
    ;
  }

  //减少按钮
  Widget _reduceBtn(context) {
    return InkWell(
      onTap: () {
        if (widget.item.realquantity > 0) {
          int a = widget.item.realquantity - 1;
          controller.text = a.toString();
          widget.item.realquantity -= 1;
          //Provider.of<ErpInfo>(context).setRealquantity(widget.index, a);
        }
      },
      child: Container(
        width: 26,
        height: 26,
        alignment: Alignment.center,
        //上下左右都居中
        decoration: BoxDecoration(
          color: Colors.transparent, //按钮颜色大于1是白色，小于1是灰色
          border: Border(
              //外层已经有边框了所以这里只设置右边的边框
              right: BorderSide(width: 1.0, color: Colors.black12)),
        ),
        child: Text('-'),
      ),
    );
  }

  //加号
  Widget _addBtn(context) {
    return InkWell(
      onTap: () {
        if (widget.item.realquantity < widget.item.quantity) {
          int a = widget.item.realquantity + 1;
          controller.text = a.toString();
          widget.item.realquantity += 1;
          //  Provider.of<ErpInfo>(context).setRealquantity(widget.index, a);
        }
      },
      child: Container(
        width: 26,
        height: 26,
        alignment: Alignment.center,
        //上下左右都居中
        decoration: BoxDecoration(
            color: Colors.transparent,
            border: Border(
                //外层已经有边框了所以这里只设置右边的边框
                left: BorderSide(width: 1.0, color: Colors.black12))),
        child: Text('+'),
      ),
    );
  }

  //中间数量显示区域
  Widget _countArea(context) {
    return Container(
        width: 58,
        height: 22,
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        alignment: Alignment.center,
        child: TextField(
          textAlign: TextAlign.center,
          decoration: InputDecoration(
              border: InputBorder.none, contentPadding: EdgeInsets.all(0)),
          style: TextStyle(fontSize: 14),
          controller: controller,
          inputFormatters: [
            WhitelistingTextInputFormatter(RegExp("[0-9]")),
            LengthLimitingTextInputFormatter(6),
          ],
          onChanged: (a) {
            print(a);
            int num = int.parse(a);
            if (num >= 0 && num <= widget.item.quantity) {
              widget.item.realquantity = num;
//              Provider.of<ErpInfo>(context).setRealquantity(widget.index, num);
            } else {
//              Provider.of<ErpInfo>(context)
//                  .setRealquantity(widget.index, widget.item.quantity);
              widget.item.realquantity = widget.item.realquantity;
              setState(() {
                controller.text = widget.item.quantity.toString();
              });
            }
          },
        ));
  }
}
